package com.woniuxy.goods.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.core.util.StateNum;
import com.woniuxy.goods.dto.BrandDto;
import com.woniuxy.goods.dto.GoodsCatDto;
import com.woniuxy.goods.entity.Brand;
import com.woniuxy.goods.mapper.BrandMapper;
import com.woniuxy.goods.param.*;
import com.woniuxy.goods.service.BrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.goods.service.GoodsBrandCatService;
import com.woniuxy.goods.service.GoodsCatService;
import com.woniuxy.goods.util.PinYinUtil;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 * 品牌管理服务层实现类
 * @author ...
 * @since 2024年05月22日
 */

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    /**
     * 拼音工具类
     */
    @Resource
    private PinYinUtil pinYinUtil;
    /**
     * 物品品牌类别配置服务层接口
     */
    @Resource
    private GoodsBrandCatService brandCatService;
    /**
     * 物品类别服务层接口
     */
    @Resource
    private GoodsCatService goodsCatService;

    /**
     * 新增品牌
     * @param param 新增品牌对象
     * @return 品牌信息
     * @throws Exception
     */
    @Override
    @GlobalTransactional(rollbackFor = Throwable.class)
    public Brand addBrand(AddBrandParam param) throws Exception {
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_name",param.getBrandName());
        Brand brand = getOne(wrapper);
        if (ObjectUtil.isNotEmpty(brand)) throw new Exception("该品牌名已存在");
        Brand bean = BeanUtil.toBean(param, Brand.class);
        String letter = pinYinUtil.getPinYinHeadChar(bean.getBrandName());
        bean.setBrandLetter(letter.charAt(0)+"");
        bean.setIsShow(StateNum.BRAND_STATE_NORMAL);
        List<Brand> brands = list();
        int size = brands.size()+1;
        String no = Integer.toString(size);
        if (no.length()<3){
            no = String.format("%03d",size);
        }
        bean.setSortNo(no);
        save(bean);
        if (ObjectUtil.isNotEmpty(param.getCatIds())){
            AddBrandCatParam param1 = new AddBrandCatParam();
            param1.setBrandId(bean.getBrandId());
            param1.setCatIds(param.getCatIds());
            brandCatService.addBrandCatConfig(param1);
        }
        return bean;
    }

    /**
     * 修改品牌
     * @param param 修改品牌对象
     * @return 品牌信息
     * @throws Exception
     */
    @Override
    @GlobalTransactional(rollbackFor = Throwable.class)
    public Brand updateBrand(UpdateBrandParam param) throws Exception {
        Brand brand = getById(param.getBrandId());
        if (ObjectUtil.isEmpty(brand)) throw new Exception("品牌不存在");
        if (!param.getBrandName().equals(brand.getBrandName())){
            QueryWrapper<Brand> wrapper = new QueryWrapper<>();
            wrapper.eq("brand_name",param.getBrandName());
            Brand name = getOne(wrapper);
            if (ObjectUtil.isNotEmpty(name)) throw new Exception("该品牌名已存在");
        }
        Brand bean = BeanUtil.toBean(param, Brand.class);
        String letter = pinYinUtil.getPinYinHeadChar(bean.getBrandName());
        bean.setBrandLetter(letter.charAt(0)+"");
        AddBrandCatParam param1 = new AddBrandCatParam();
        param1.setBrandId(param.getBrandId());
        param1.setCatIds(param.getCatIds());
        brandCatService.updateBrandCatByBrandId(param1);
        updateById(bean);
        return bean;
    }

    /**
     * 条件查询品牌
     * @param param 条件查询品牌对象
     * @return 分页品牌信息
     * @throws Exception
     */
    @Override
    public IPage<BrandDto> findBrands(FindBrandsParam param) {
        Page<Brand> page = new Page<>(param.getPageNum(), param.getPageSize());
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort_no");
        if (ObjectUtil.isNotEmpty(param.getBrandName())){
            wrapper.like("brand_name",param.getBrandName());
            wrapper.or();
            wrapper.like("brand_letter",param.getBrandLetter());
            wrapper.or();
        }
        if (ObjectUtil.isNotEmpty(param.getBrandLetter())){
            wrapper.like("brand_name",param.getBrandLetter());
            wrapper.or();
            wrapper.like("brand_letter",param.getBrandLetter());
        }
        page(page,wrapper);
        IPage<BrandDto> iPage = page.convert(v -> {
            BrandDto dto = new BrandDto();
            BeanUtil.copyProperties(v, dto);
            return dto;
        });
        List<BrandDto> records = iPage.getRecords();
        for (BrandDto record : records) {
            if (record.getIsShow()==StateNum.BRAND_STATE_NORMAL){
                record.setState("启用");
            }else {
                record.setState("禁用");
            }
            Set<Integer> ids = brandCatService.findCatIdsByBrandId(record.getBrandId());
            if(ObjectUtil.isNotEmpty(ids)){
                List<GoodsCatDto> cat = goodsCatService.findCatByIds(ids);
                record.setList(cat);
            }
        }
        return iPage;
    }

    /**
     * 通过id查询品牌
     * @param brandId 品牌id
     * @return 品牌信息
     * @throws Exception
     */
    @Override
    public Brand findBrandById(Integer brandId) {
        return getById(brandId);
    }

    /**
     * 通过ids查询品牌
     * @param brandIds 品牌id集合
     * @return 品牌信息集合
     * @throws Exception
     */
    @Override
    public List<Brand> findBrandByIds(Set<Integer> brandIds) {
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        wrapper.in("brand_id",brandIds);
        return list(wrapper);
    }



}
